using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace CreatingRainbows
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        SpriteFont rainbowFont;

        int screenHeight;
        public int ScreenHeight
        {
            get { return this.screenHeight; }
        }
        int screenWidth;
        public int ScreenWidth
        {
            get { return this.screenWidth; }
        }

        Vector2 startingPos;

        protected Texture2D mainRoomBackground;
        protected Texture2D mainRoomForeground;

        protected Texture2D magentaRoomBackground;
        protected Texture2D magentaRoomForeground;

        protected Texture2D iSpyRoomBackground;
        protected Texture2D iSpyRoomForeground;

        protected Texture2D iSpyRoomBackground1;
        protected Texture2D iSpyRoomForeground1;

        protected MainRoom mainRoomScene;
        public MainRoom MainRoomScene
        {
            get { return mainRoomScene; }
        }

        protected MagentaRoom magentaRoomScene;
        public MagentaRoom MagentaRoomScene
        {
            get { return magentaRoomScene; }
        }

        protected ISpyRoom iSpyRoomScene;
        public ISpyRoom ISpyRoomScene
        {
            get { return iSpyRoomScene; }
        }

        protected ISpyRoom1 iSpyRoomScene1;
        public ISpyRoom1 ISpyRoomScene1
        {
            get { return iSpyRoomScene1; }
        }

        protected GameScene activeScene;
        public GameScene ActiveScene
        {
            get { return activeScene; }
            set { activeScene = value; }
        }

        # region unused variables
        //bool isKey1Pressed;
        //bool wasKey1Pressed;

        //bool isKey2Pressed;
        //bool wasKey2Pressed;

        //KeyboardState keyboard;

        #endregion

        public const int SCENE_WIDTH = 500;
        public int SceneWidth
        {
            get { return SCENE_WIDTH; }
        }

        public const int SCENE_HEIGHT = 400;
        public int SceneHeight
        {
            get { return SCENE_HEIGHT; }
        }

        int foregroundX;
        public int ForegroundX
        {
            get { return foregroundX; }
        }

        int foregroundY;
        public int ForegroundY
        {
            get { return foregroundY; }
        }


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.IsFullScreen = false;
            Content.RootDirectory = "Content";
            //Components.Add(new GamerServicesComponent(this));
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {

            spriteBatch = new SpriteBatch(GraphicsDevice);

            screenHeight = graphics.GraphicsDevice.Viewport.Height;
            screenWidth = graphics.GraphicsDevice.Viewport.Width;

            foregroundX = (ScreenWidth / 2) - (SceneWidth / 2);
            foregroundY = (ScreenHeight / 2) - (SceneHeight / 2);

            this.startingPos = new Vector2(screenWidth / 2, screenHeight / 2);

            #region unused initialize
            //isKey1Pressed = false;
            //wasKey1Pressed = false;

            //isKey2Pressed = false;
            //wasKey2Pressed = false;
            #endregion   

            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Services.AddService(typeof(SpriteBatch), spriteBatch);

            mainRoomBackground = Content.Load<Texture2D>("monochromeRoom");
            mainRoomForeground = Content.Load<Texture2D>("monochromeRoomFore");

            magentaRoomBackground = Content.Load<Texture2D>("magentaRoom");
            magentaRoomForeground = Content.Load<Texture2D>("magentaRoomFore");

            iSpyRoomBackground = Content.Load<Texture2D>("puzzleRoom");
            iSpyRoomForeground = Content.Load<Texture2D>("monochromeRoom");

            iSpyRoomBackground1 = Content.Load<Texture2D>("puzzleRoom");
            iSpyRoomForeground1 = Content.Load<Texture2D>("monochromeRoom");

            mainRoomScene = new MainRoom(this, mainRoomBackground, mainRoomForeground);
            magentaRoomScene = new MagentaRoom(this, magentaRoomBackground, magentaRoomForeground);
            iSpyRoomScene = new ISpyRoom(this, iSpyRoomBackground, iSpyRoomForeground);
            iSpyRoomScene1 = new ISpyRoom1(this, iSpyRoomBackground, iSpyRoomForeground);

            Components.Add(mainRoomScene);
            Components.Add(magentaRoomScene);
            Components.Add(iSpyRoomScene);
            Components.Add(iSpyRoomScene1);
            
            mainRoomScene.Show();
            magentaRoomScene.Hide();
            iSpyRoomScene.Hide();
            iSpyRoomScene1.Hide();

            activeScene = mainRoomScene;

            rainbowFont = Content.Load<SpriteFont>("RainbowFont");



            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            //HandleInput();
            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        //private void HandleInput()
        //{
        //    keyboard = Keyboard.GetState();
        //    isKey1Pressed = keyboard.IsKeyDown(Keys.D1);
        //    isKey2Pressed = keyboard.IsKeyDown(Keys.D2);

        //    //for mainRoom
        //    if(!isKey1Pressed && wasKey1Pressed)
        //    {
        //        magentaRoomScene.Hide();
        //        mainRoomScene.Show();

        //        activeScene = mainRoomScene;
        //    }
        //    wasKey1Pressed = isKey1Pressed;

        //    //for magentaRoom
        //    if (!isKey2Pressed && wasKey2Pressed)
        //    {
        //        mainRoomScene.Hide();
        //        magentaRoomScene.Show();

        //        activeScene = magentaRoomScene;
        //    }
        //    wasKey2Pressed = isKey2Pressed;

        //}

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            //spriteBatch.Begin();

            //spriteBatch.DrawString(rainbowFont, "height " + screenHeight + "width" + screenWidth, new Vector2(90, 90), Color.White);
            // TODO: Add your drawing code here

            //spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
